const Router = require('koa-router')
const {HttpException}=require('../cors/http-exception')
const {PositiveIntegerValidator}=require("../validators/validator")
const {
  Flow
} = require('@models/flow')
const {
  Favor
} = require('@models/favor')
const {
  Art
} = require('../models/Art')
const {Auth}=require("../middlewares/auth")
const router = new Router({
    prefix: '/v1/classic'
})
router.get('/latest', new Auth().m, async (ctx, next) => {
    //权限 复杂
    //限制token 角色
    //业务分普通用户 系统管理员
    //分级 scope
    //普通用户 8 16

    const flow = await Flow.findOne({ //order排序 //DESC倒叙排列
      order: [
          ['index', 'DESC'] 
      ]
  })
 
  
  let art = await Art.getData(flow.art_id, flow.type);
  // const likeLatest = await Favor.userLikeIt(flow.art_id, flow.type, ctx.auth.uid);

  art.setDataValue('index', flow.index);
  // art.setDataValue('like_status', likeLatest);

  ctx.body = {
      art
  }
//   const body=ctx.request.body
//   const query=ctx.request.query //获取url?后面参数


//   if(query){
//    // const error=new HttpException('query is kong',10001,400)
//  //global  有错误的话比较难排查   已知异常
//  const error =new global.errs.ParameterException('query is kong',10001,400)
//     throw error
//   }
  
//   ctx.body ='koa2 string'
})
router.get('/:index/next', new Auth().m, async (ctx) => {
  const v = await new PositiveIntegerValidator().validate(ctx, {
      id: 'index'
  })
  const index = v.get('path.index')
  const flow = await Flow.findOne({
      where: {
          index: index + 1
      }
  })
  if (!flow) {
      throw new global.errs.NotFound()
  }
  const art = await Art.getData(flow.art_id, flow.type)
  const likeNext = await Favor.userLikeIt(
      flow.art_id, flow.type, ctx.auth.uid)
  art.setDataValue('index', flow.index)
  art.setDataValue('like_status', likeNext)
  ctx.body = art
})

router.get('/:index/previous', new Auth().m, async (ctx) => {
  const v = await new PositiveIntegerValidator().validate(ctx, {
      id: 'index'
  })
  const index = v.get('path.index')
  const flow = await Flow.findOne({
      where: {
          index: index - 1
      }
  })
  if (!flow) {
      throw new global.errs.NotFound()
  }
  const art = await Art.getData(flow.art_id, flow.type)
  const likePrevious = await Favor.userLikeIt(
  flow.art_id, flow.type, ctx.auth.uid)
  art.setDataValue('index', flow.index)
  art.setDataValue('like_status', likePrevious)
  ctx.body = art
})
router.get('/:type/:id/favor', new Auth().m, async ctx => {
  const v = await new ClassicValidator().validate(ctx)
  const id = v.get('path.id')
  const type = parseInt(v.get('path.type'))

  const artDetail =await new Art(id,type).getDetail(ctx.auth.uid)

  ctx.body = {
      fav_nums: artDetail.art.fav_nums,
      like_status: artDetail.like_status
  }
})
router.get('/:type/:id', new Auth().m, async ctx=>{
  const v = await new ClassicValidator().validate(ctx)
  const id = v.get('path.id')
  const type = parseInt(v.get('path.type'))

  const artDetail =await new Art(id,type).getDetail(ctx.auth.uid)

  artDetail.art.setDataValue('like_status', artDetail.like_status)
  ctx.body = artDetail.art
})
router.get('/favor', new Auth().m, async ctx => {
  const uid = ctx.auth.uid
  ctx.body = await Favor.getMyClassicFavors(uid)
})

router.post('/json/:id/my', async (ctx, next) => {

  ctx.params .id=  Number(ctx.params.id)
  const path=ctx.params ///v1/:id/c获取id
  // console.log(path)
  const v=new PositiveIntegerValidator().validate(ctx) //校验 获取数据
  
 
  ctx.body=  v.get('path.id')  //校验器会自动转换格式    ctx.body=  v.get('path.id parsed=false') 不转换
  const c={
    a:1,
    b:{
      f:2,
      e:{
        o:2 
      }
    }
  }
  //  ctx.body=  v.get('body.b.e')  可以免除判断 是否存在等 
  //User表格 一个小程序 一个通用行的
  //账号 密码 名称 手机号码 
  //注册 
  // Sequelize 连接数据库 配置一些数据库的相关数据
})

module.exports = router
